﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using Tao.FreeGlut;
using Phys.Model;
using TWiStEr.Math; // Vector

namespace Phys.UI {
	public partial class PhysicsGLWindow {
		/// <summary>
		/// Keyboard movement speed in centimeters, used in GL_Keyboard
		/// </summary>
		private double speed = .2;
		private void GL_Keyboard(byte key, int nMouseX, int nMouseY) {
			switch ((char)key) {
				case (char)27:
				case 'q': Close(); break;
				case 'w': cam.move(speed, false); break;
				case 's': cam.move(-speed, false); break;
				case 'd': cam.move(speed, true); break;
				case 'a': cam.move(-speed, true); break;
				case ' ': cam.MoveVertical(speed); break;
				case 'c': cam.MoveVertical(-speed); break;
				case 'n': cam.AlwaysLookAt = new Vector(0, 0, 0); break;
				case 'm': cam.IsLockedAtCenterPosition = false; break;
				case '+': speed += 0.01; break;
				case '-': speed -= 0.01; break;
				case 'f': cam.SwitchFullScreen(); break;
				case '\r': case '\n': if (World.IsStarted) World.Stop(); else World.Start(); break;
			}
			cam.glSetMatrices();
			Glut.glutPostRedisplay();
		}

		/// <summary>
		/// Previous mouse coordinates, used in GL_MouseMove
		/// </summary>
		private int prevpx = 0, prevpy = 0;
		/// <summary>
		/// Tells the camera how much is the movement done by the mouse
		/// </summary>
		/// <param name="x">mouse current x coordinate</param>
		/// <param name="y">mouse current y coordinate</param>
		private void GL_MouseMove(int x, int y) {
			if ((prevpx - x) == 0 && (prevpy - y) == 0) return;
			if (prevpx != 0 || prevpy != 0)
				cam.FollowMouse(prevpx - x, prevpy - y);
			GL_PassiveMouseMove(x, y);
			cam.glSetMatrices();
			Glut.glutPostRedisplay();
		}
		private void GL_PassiveMouseMove(int x, int y) {
			prevpx = x;
			prevpy = y;
		}

		private void GL_Special(int nSpecial, int nMouseX, int nMouseY) {
			switch (nSpecial) {
				case Glut.GLUT_KEY_F4: Close(); break;
			}
			Debug.WriteLine(string.Format("SampleSpecial(): special keypress {0} at ({1},{2})\n", nSpecial, nMouseX, nMouseY));
		}

		private void GL_Menu(int menuID) {
			Debug.WriteLine(string.Format("SampleMenu() callback executed, menuID is {0}\n", menuID));
		}
	}
}
